package com.example.imoocmusicdemo.activitys;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.example.imoocmusicdemo.R;
import com.example.imoocmusicdemo.helps.DBOpenHelper;
import com.example.imoocmusicdemo.utils.User;

import java.util.ArrayList;

public class DeleteUser extends BaseActivity {

    private EditText number,password,password2;
    private TextView btn;

    private DBOpenHelper mDB;

    @Override
    protected void onCreate(Bundle savedInstanceState) {




        mDB = new DBOpenHelper(this);

        super.onCreate(savedInstanceState);
        setContentView(R.layout.delete_user);

        number = fd(R.id.number);
        password = fd(R.id.password);
        password2 = fd(R.id.password2);
        btn = fd(R.id.Btn1);
        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String name = number.getText().toString().trim();
                String pwd = password.getText().toString().trim();
                String pwd2 = password2.getText().toString().trim();
                ArrayList<User> data = mDB.getAllData();
                for (int i = 0; i < data.size(); i++) {
                    User user = data.get(i);   //可存储账号数量
                    Log.i("TAG", user.getName() + "_" + user.getPassword() + name.equals(user.getName()) + name);
                    if (name.equals(user.getName()) && pwd.equals(user.getPassword()) && pwd2.equals(user.getPassword())) {
                        mDB.delete(name, pwd);
                        Toast.makeText(DeleteUser.this, "删除成功！已退出账号", Toast.LENGTH_SHORT).show();
                        Intent intent1 = new Intent(DeleteUser.this, LoginActivity.class);
                        startActivity(intent1);
                        finish();
                        break;
                    } else if (name != user.getName() && pwd != user.getPassword() && pwd2 != user.getPassword()) {
                        Toast.makeText(DeleteUser.this, "用户名或密码不正确", Toast.LENGTH_SHORT).show();
                    }

                }
            }
        });
        init();
    }

    private void init() {
        initNavBar(true,"emo期待您再次光临",false);
    }
}